/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#pragma once

#include <turbo/container/flat_hash_map.h>
#include <kmhdfs/common/thread.h>

#include <list>

namespace Hdfs {
    namespace Internal {
        template<typename K, typename V>
        class LruMap {
        public:
            typedef K KeyType;
            typedef V ValueType;
            typedef std::pair<K, V> ItmeType;
            typedef std::list<ItmeType> ListType;
            typedef turbo::flat_hash_map<K, typename ListType::iterator> MapType;

        public:
            LruMap() : count(0), maxSize(1000) {
            }

            LruMap(size_t size) : count(0), maxSize(size) {
            }

            ~LruMap() {
                lock_guard<mutex> lock(mut);
                map.clear();
                list.clear();
            }

            void setMaxSize(size_t s) {
                lock_guard<mutex> lock(mut);
                maxSize = s;

                for (size_t i = count; i > s; --i) {
                    map.erase(list.back().first);
                    list.pop_back();
                    --count;
                }
            }

            void insert(const KeyType &key, const ValueType &value) {
                lock_guard<mutex> lock(mut);
                typename MapType::iterator it = map.find(key);

                if (it != map.end()) {
                    --count;
                    list.erase(it->second);
                }

                list.push_front(std::make_pair(key, value));
                map[key] = list.begin();
                ++count;

                if (count > maxSize) {
                    map.erase(list.back().first);
                    list.pop_back();
                    --count;
                }
            }

            void erase(const KeyType &key) {
                lock_guard<mutex> lock(mut);
                typename MapType::iterator it = map.find(key);

                if (it != map.end()) {
                    list.erase(it->second);
                    map.erase(it);
                    --count;
                }
            }

            bool find(const KeyType &key, ValueType *value) {
                lock_guard<mutex> lock(mut);
                return findAndEraseInternal(key, value, false);
            }

            bool findAndErase(const KeyType &key, ValueType *value) {
                lock_guard<mutex> lock(mut);
                return findAndEraseInternal(key, value, true);
            }

            size_t size() {
                lock_guard<mutex> lock(mut);
                return count;
            }

        private:
            bool findAndEraseInternal(const KeyType &key, ValueType *value,
                                      bool erase) {
                typename MapType::iterator it = map.find(key);

                if (it != map.end()) {
                    *value = it->second->second;
                    list.erase(it->second);

                    if (erase) {
                        map.erase(it);
                        --count;
                    } else {
                        list.push_front(std::make_pair(key, *value));
                        map[key] = list.begin();
                    }

                    return true;
                }

                return false;
            }

        private:
            size_t count;
            size_t maxSize;
            ListType list;
            MapType map;
            mutex mut;
        };
    }
}
